import { getLength, isArrayLike } from "../collections/each"
import isArray from "../object/isArray";
import isArguments from "../object/isArguments";

const flatten = []

export function f1(input: [], shallow, strict, output = []) {
    var idx = output.length
    for (var i = 0, length = getLength(input); i < length; i++) {
        var value: any = input[i];
        if (isArrayLike(value) && (isArray(value) || isArguments(value))) {
            // Flatten current level of array or arguments object.
            if (shallow) {
                var j = 0, len = value.length;
                while (j < len) output[idx++] = value[j++];
            } else {
                f1(value, shallow, strict, output);
                idx = output.length;
            }
        } else if (!strict) {
            output[idx++] = value;
        }
    }
    return output;
}

function f2(array: any[]) {
    let arr = []
    for (let v of array) {
        if (Array.isArray(v)) {
            arr = arr.concat([...f2(v)])
        } else {
            arr.push(v)
        }
    }
    return arr
}

function* f3(array) {
    let arr = []
    for (let v of array) {
        if (Array.isArray(v)) {
            arr = [...arr, ...f3(v)]
        } else {
            arr.push(v)
        }
    }
    yield arr
}

export default flatten